package com.itchu.system.controller;

import com.itchu.common.domain.PageResult;
import com.itchu.system.domain.Song;
import com.itchu.system.domain.SongList;
import com.itchu.system.service.SongListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/songlist")
public class SongListController {

    @Autowired
    private SongListService songListService;

    /**
     * ---------------------------------------
     * ================================
     * 查找歌单的分页信息
     * @param key   搜索条件
     * @param page  当前页
     * @param rows  页大小
     * @param sortBy
     * @param desc
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<PageResult<SongList>> findByPage(
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "sortBy", required = false) String sortBy,
            @RequestParam(value = "desc", required = false) Boolean desc
    ) {
        PageResult<SongList> list = songListService.findByPage(key,page,rows,sortBy,desc);
        return ResponseEntity.ok(list);
    }

    /**
     * ------------------------------------
     * 添加歌单操作
     * @param songList  歌单对象
     * @param cids   分类ids
     * @return
     */
    @PostMapping("/saveSongList")
    public ResponseEntity<Void> addSongList(
            SongList songList,
            @RequestParam("cids")List<Long> cids) {
        songListService.addSongList(songList,cids);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * ---------------------------------
     * 修改歌单操作
     * @param songList  歌单对象
     * @param cids   分类ids
     * @return
     */
    @PutMapping("/saveSongList")
    public ResponseEntity<Void> updateSongList(
            SongList songList,
            @RequestParam("cids")List<Long> cids) {
        songListService.updateSongList(songList,cids);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * --------------------------------------------
     * 删除歌单操作
     * @param id  歌单id
     * @return
     */
    @GetMapping("/deleteSongList")
    public ResponseEntity<Void> deletSongList(
            @RequestParam("id")Long id) {

        songListService.deletSongList(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * -------------------------------
     * ==============================
     * 根据歌单的id查找歌曲列表信息
     * @param id  歌单id
     * @return
     */
    @GetMapping("/list/song")
    public ResponseEntity<List<Song>> findListSongByLid(
            @RequestParam("id") Long id
    ){
        List<Song> list = songListService.findListSongByLid(id);
        return ResponseEntity.ok(list);
    }

    /**
     * --------------------------------
     * 给歌单添加歌曲操作
     * @param id  歌单id
     * @param sids   歌曲ids
     * @return
     */
    @PostMapping("/list/addSong")
    public ResponseEntity<Void> addSongToList(
            @RequestParam("id") Long id,
            @RequestParam("sids") List<Long> sids
    ) {
        songListService.addSongToList(id,sids);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * ----------------------------------------
     * 在歌单列表删除多个歌曲
     * @param id  歌单id
     * @param sids  歌曲ids
     * @return
     */
    @RequestMapping("/song/deleteMutiple")
    public ResponseEntity<Void> deleteMutipleSong(@RequestParam("id") Long id,
                                           @RequestParam("sids") List<Long> sids) {
        songListService.deleteMutipleSong(id,sids);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 在歌单列表删除单个歌曲
     * @param id  歌单id
     * @param sid  歌曲id
     * @return
     */
    @RequestMapping("/song/delete")
    public ResponseEntity<Void> deleteSong(@RequestParam("id") Long id,
                                                  @RequestParam("sid") Long sid) {
        songListService.deleteSong(id,sid);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

//    /**
//     * =======================
//     * 根据分类的id查找歌单列表
//     * @param page     当前页
//     * @param rows     页大小
//     * @param cid      分类id
//     * @param isParent  是否是一级分类
//     * @return
//     */
//    @GetMapping("/of/category")
//    public ResponseEntity<PageResult<SongList>> findSongListByCategoryId(
//            @RequestParam(value = "page",defaultValue = "1") Integer page,
//            @RequestParam(value = "rows",defaultValue = "25") Integer rows,
//            @RequestParam(value = "cid") Long cid,
//            @RequestParam(value = "isParent") Long isParent
//    ) {
//        PageResult<SongList> list = songListService.findSongListByCategoryId(page,rows,cid,isParent);
//
//        return ResponseEntity.ok(list);
//    }

//    /**
//     * ====================================
//     * 按名字做模糊搜索
//     * @param name
//     * @return
//     */
//    @GetMapping("search/SongLists")
//    public ResponseEntity<List<SongList>> searchSongListsByName(
//            @RequestParam(value = "name") String name
//    ) {
//        List<SongList> list = songListService.searchSongListsByName(name);
//
//        return ResponseEntity.ok(list);
//    }
}
